
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>优化开发 · test search plus plugin</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Hot-loading-components.html" />
    
    
    <link rel="prev" href="Wing-It-Like-a-Pro.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="Introduction-to-Webpack.html">
            
                <a href="Introduction-to-Webpack.html">
            
                    
                    介绍 Webpack
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="Getting-started.html">
            
                <a href="Getting-started.html">
            
                    
                    第一步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="Running-a-workflow.html">
            
                <a href="Running-a-workflow.html">
            
                    
                    开始工作流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="Automatic-browser-refresh.html">
            
                <a href="Automatic-browser-refresh.html">
            
                    
                    浏览器自动刷新
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="Requiring-files.html">
            
                <a href="Requiring-files.html">
            
                    
                    引入文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Introduction-to-React-JS.html">
            
                <a href="Introduction-to-React-JS.html">
            
                    
                    React JS
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="Configuring-react-js.html">
            
                <a href="Configuring-react-js.html">
            
                    
                    配置 React JS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="Optimizing-rebundling.html">
            
                <a href="Optimizing-rebundling.html">
            
                    
                    优化重合并
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="Type-checking-with-flow.html">
            
                <a href="Type-checking-with-flow.html">
            
                    
                    Flow
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="CSS-Fonts-Images.md">
            
                <span>
            
                    
                    CSS, Fonts and Images
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="Loading-CSS.html">
            
                <a href="Loading-CSS.html">
            
                    
                    加载 CSS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="Automatic-CSS-refresh.html">
            
                <a href="Automatic-CSS-refresh.html">
            
                    
                    自动刷新 CSS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="Loading-LESS-or-SASS.html">
            
                <a href="Loading-LESS-or-SASS.html">
            
                    
                    加载 LESS 和 SASS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="Inlining-images.html">
            
                <a href="Inlining-images.html">
            
                    
                    内联 images
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="Inlining-fonts.html">
            
                <a href="Inlining-fonts.html">
            
                    
                    内联 fonts
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="Deployment-strategies.md">
            
                <span>
            
                    
                    部署策略
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="Structuring-configuration.html">
            
                <a href="Structuring-configuration.html">
            
                    
                    发布配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="Single-bundle.html">
            
                <a href="Single-bundle.html">
            
                    
                    合并成单文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="Split-app-and-vendors.html">
            
                <a href="Split-app-and-vendors.html">
            
                    
                    分离应用和第三方
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="Multiple-entry-points.html">
            
                <a href="Multiple-entry-points.html">
            
                    
                    多重入口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="Lazy-loaded-entry-points.html">
            
                <a href="Lazy-loaded-entry-points.html">
            
                    
                    懒加载入口文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="Isomorphic-app.html">
            
                <a href="Isomorphic-app.html">
            
                    
                    同构渲染
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="Wing-It-Like-a-Pro.html">
            
                <a href="Wing-It-Like-a-Pro.html">
            
                    
                    进阶
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.6.1" data-path="Optimizing-development.html">
            
                <a href="Optimizing-development.html">
            
                    
                    优化开发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="Hot-loading-components.html">
            
                <a href="Hot-loading-components.html">
            
                    
                    热加载组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="JavaScript-next.html">
            
                <a href="JavaScript-next.html">
            
                    
                    使用下一代 JavaScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="Optimizing-caching.html">
            
                <a href="Optimizing-caching.html">
            
                    
                    优化缓存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="Matchers.html">
            
                <a href="Matchers.html">
            
                    
                    匹配器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="Lazy-loading-entry-points.md">
            
                <span>
            
                    
                    懒加载入口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="Creating-a-common-bundle.md">
            
                <span>
            
                    
                    创建一个复用代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="Understanding-chunks.html">
            
                <a href="Understanding-chunks.html">
            
                    
                    理解 Chunks
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="Authoring-libraries.html">
            
                <a href="Authoring-libraries.html">
            
                    
                    创建库
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >优化开发</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>We talked about how you could use the minified versions of your dependencies in development to make the rebundling go as fast as possible. Let us look at a small helper you can implement to make this a bit easier to handle.</p>
<p>&#x4E4B;&#x524D;&#x4ECB;&#x7ECD;&#x4E86;&#x5982;&#x4F55;&#x5728;&#x5F00;&#x53D1;&#x4E2D;&#x4F7F;&#x7528;&#x4F9D;&#x8D56;&#x7684;&#x538B;&#x7F29;&#x7248;&#x672C;&#x6765;&#x8BA9;&#x5408;&#x5E76;&#x5C3D;&#x53EF;&#x80FD;&#x52A0;&#x901F;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x4E00;&#x4E0B;&#x8FD9;&#x4E2A;&#x5C0F;&#x7684;&#x4F8B;&#x5B50;&#x6765;&#x8BA9;&#x4F60;&#x66F4;&#x52A0;&#x8F7B;&#x677E;&#x53BB;&#x5904;&#x7406;&#xFF1A;</p>
<p><em>webpack.config.js</em></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> webpack = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;webpack&apos;</span>);
<span class="hljs-keyword">var</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">var</span> node_modules_dir = path.join(__dirname, <span class="hljs-string">&apos;node_modules&apos;</span>);

<span class="hljs-keyword">var</span> deps = [
  <span class="hljs-string">&apos;react/dist/react.min.js&apos;</span>,
  <span class="hljs-string">&apos;react-router/dist/react-router.min.js&apos;</span>,
  <span class="hljs-string">&apos;moment/min/moment.min.js&apos;</span>,
  <span class="hljs-string">&apos;underscore/underscore-min.js&apos;</span>,
];

<span class="hljs-keyword">var</span> config = {
  entry: [<span class="hljs-string">&apos;webpack/hot/dev-server&apos;</span>, <span class="hljs-string">&apos;./app/main.js&apos;</span>],
  output: {
    path: path.resolve(__dirname, <span class="hljs-string">&apos;./build&apos;</span>),
    filename: <span class="hljs-string">&apos;bundle.js&apos;</span>
  },
  resolve: {
    alias: {}
  },
  <span class="hljs-built_in">module</span>: {
    noParse: [],
    loaders: []
  }
};

<span class="hljs-comment">// Run through deps and extract the first part of the path, </span>
<span class="hljs-comment">// as that is what you use to require the actual node modules </span>
<span class="hljs-comment">// in your code. Then use the complete path to point to the correct</span>
<span class="hljs-comment">// file and make sure webpack does not try to parse it</span>
<span class="hljs-comment">// &#x901A;&#x8FC7;&#x5728;&#x7B2C;&#x4E00;&#x90E8;&#x5206;&#x8DEF;&#x5F84;&#x7684;&#x4F9D;&#x8D56;&#x548C;&#x89E3;&#x538B;</span>
<span class="hljs-comment">// &#x5C31;&#x662F;&#x4F60;&#x50CF;&#x5F15;&#x7528; node &#x6A21;&#x5757;&#x4E00;&#x6837;&#x5F15;&#x5165;&#x5230;&#x4F60;&#x7684;&#x4EE3;&#x7801;&#x4E2D;</span>
<span class="hljs-comment">// &#x7136;&#x540E;&#x4F7F;&#x7528;&#x5B8C;&#x6574;&#x8DEF;&#x5F84;&#x6307;&#x5411;&#x5F53;&#x524D;&#x6587;&#x4EF6;&#xFF0C;&#x7136;&#x540E;&#x786E;&#x8BA4; Webpack &#x4E0D;&#x4F1A;&#x5C1D;&#x8BD5;&#x53BB;&#x89E3;&#x6790;&#x5B83;</span>

deps.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">dep</span>) </span>{
  <span class="hljs-keyword">var</span> depPath = path.resolve(node_modules_dir, dep);
  config.resolve.alias[dep.split(path.sep)[<span class="hljs-number">0</span>]] = depPath;
  config.module.noParse.push(depPath);
});

<span class="hljs-built_in">module</span>.exports = config;
</code></pre>
<p>Not all modules include a minified distributed version of the lib, but most do. Especially with large libraries like React JS you will get a significant improvement.</p>
<p>&#x4E0D;&#x662F;&#x6240;&#x6709;&#x7684;&#x6A21;&#x5757;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x538B;&#x7F29;&#x7684;&#x7248;&#x672C;&#xFF0C;&#x4E0D;&#x8FC7;&#x5927;&#x591A;&#x6570;&#x9700;&#x8981;&#xFF0C;&#x5C24;&#x5176;&#x662F;&#x50CF; React JS &#x8FD9;&#x79CD;&#x5927;&#x578B;&#x5E93;&#xFF0C;&#x4E4B;&#x540E;&#x4F60;&#x4F1A;&#x6709;&#x660E;&#x663E;&#x7684;&#x63D0;&#x5347;&#x3002;</p>
<h2 id="&#x628A;-react-&#x66B4;&#x9732;&#x5230;&#x5168;&#x5C40;&#x4E2D;">&#x628A; React &#x66B4;&#x9732;&#x5230;&#x5168;&#x5C40;&#x4E2D;</h2>
<p>You might be using distributed versions that requires React JS on the global scope. To fix that you can install the expose-loader by <code>npm install expose-loader --save-dev</code> and set up the following config, focusing on the <em>module</em> property:</p>
<p>&#x4F60;&#x53EF;&#x80FD;&#x5728;&#x5168;&#x5C40;&#x4E2D;&#x4F7F;&#x7528;&#x4E86;&#x4E00;&#x4E2A;&#x538B;&#x7F29;&#x7248;&#x672C;&#x7684; React&#xFF0C;&#x4E3A;&#x4E86;&#x4FEE;&#x590D;&#x4F60;&#x53EF;&#x4EE5;&#x5B89;&#x88C5;&#x8FD9;&#x4E2A;&#x66B4;&#x9732;&#x5168;&#x5C40;&#x52A0;&#x8F7D;&#x5668; <code>npm install expose-loader --save-dev</code>&#xFF0C;&#x7136;&#x540E;&#x50CF;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x914D;&#x7F6E;&#xFF0C;&#x6CE8;&#x610F; <em>module</em> &#x5C5E;&#x6027;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> webpack = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;webpack&apos;</span>);
<span class="hljs-keyword">var</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">var</span> node_modules_dir = path.join(__dirname, <span class="hljs-string">&apos;node_modules&apos;</span>);

<span class="hljs-keyword">var</span> deps = [
  <span class="hljs-string">&apos;react/dist/react.min.js&apos;</span>,
  <span class="hljs-string">&apos;react-router/dist/react-router.min.js&apos;</span>,
  <span class="hljs-string">&apos;moment/min/moment.min.js&apos;</span>,
  <span class="hljs-string">&apos;underscore/underscore-min.js&apos;</span>,
];

<span class="hljs-keyword">var</span> config = {
  entry: [<span class="hljs-string">&apos;webpack/hot/dev-server&apos;</span>, <span class="hljs-string">&apos;./app/main.js&apos;</span>],
  output: {
    path: path.resolve(__dirname, <span class="hljs-string">&apos;./build&apos;</span>),
    filename: <span class="hljs-string">&apos;bundle.js&apos;</span>
  },
  resolve: {
    alias: {}
  },
  <span class="hljs-built_in">module</span>: {
    noParse: [],

    <span class="hljs-comment">// Use the expose loader to expose the minified React JS</span>
    <span class="hljs-comment">// distribution. For example react-router requires this</span>
    <span class="hljs-comment">// &#x4F7F;&#x7528;&#x66B4;&#x9732;&#x5168;&#x5C40;&#x52A0;&#x8F7D;&#x5668;&#x6765;&#x66B4;&#x9732;&#x538B;&#x7F29;&#x7248;&#x7684; React JS&#xFF0C;&#x6BD4;&#x5982; react-router &#x9700;&#x8981;&#x8FD9;&#x4E2A;&#x3002;</span>
    loaders: [{
      test: path.resolve(node_modules_dir, deps[<span class="hljs-number">0</span>]),
      loader: <span class="hljs-string">&quot;expose?React&quot;</span>
    }]
  }
};

deps.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">dep</span>) </span>{
  <span class="hljs-keyword">var</span> depPath = path.resolve(node_modules_dir, dep);
  config.resolve.alias[dep.split(path.sep)[<span class="hljs-number">0</span>]] = depPath;
  config.module.noParse.push(depPath);
});

<span class="hljs-built_in">module</span>.exports = config;
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Wing-It-Like-a-Pro.html" class="navigation navigation-prev " aria-label="Previous page: 进阶">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Hot-loading-components.html" class="navigation navigation-next " aria-label="Next page: 热加载组件">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"优化开发","level":"1.6.1","depth":2,"next":{"title":"热加载组件","level":"1.6.2","depth":2,"path":"Hot-loading-components.md","ref":"Hot-loading-components.md","articles":[]},"previous":{"title":"进阶","level":"1.6","depth":1,"path":"Wing-It-Like-a-Pro.md","ref":"Wing-It-Like-a-Pro.md","articles":[{"title":"优化开发","level":"1.6.1","depth":2,"path":"Optimizing-development.md","ref":"Optimizing-development.md","articles":[]},{"title":"热加载组件","level":"1.6.2","depth":2,"path":"Hot-loading-components.md","ref":"Hot-loading-components.md","articles":[]},{"title":"使用下一代 JavaScript","level":"1.6.3","depth":2,"path":"JavaScript-next.md","ref":"JavaScript-next.md","articles":[]},{"title":"优化缓存","level":"1.6.4","depth":2,"path":"Optimizing-caching.md","ref":"Optimizing-caching.md","articles":[]},{"title":"匹配器","level":"1.6.5","depth":2,"path":"Matchers.md","ref":"Matchers.md","articles":[]},{"title":"懒加载入口","level":"1.6.6","depth":2,"path":"Lazy-loading-entry-points.md","ref":"Lazy-loading-entry-points.md","articles":[]},{"title":"创建一个复用代码","level":"1.6.7","depth":2,"path":"Creating-a-common-bundle.md","ref":"Creating-a-common-bundle.md","articles":[]},{"title":"理解 Chunks","level":"1.6.8","depth":2,"path":"Understanding-chunks.md","ref":"Understanding-chunks.md","articles":[]},{"title":"创建库","level":"1.6.9","depth":2,"path":"Authoring-libraries.md","ref":"Authoring-libraries.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-pro","fontsettings","livereload"],"root":"./content","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"search-pro":{},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{"version":"0.0.0"},"title":"test search plus plugin","gitbook":"*"},"file":{"path":"Optimizing-development.md","mtime":"2017-01-24T08:51:37.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-01-24T08:53:13.796Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    

    </body>
</html>

